home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / lfcr.zip / LFCR.ASM < prev    next >
Assembly Source File  |  1986-09-21  |  7KB  |  271 lines

  1.     title "Replace LineFeed or Carriage Return with CR/LF."
  2.  
  3. filefix    segment
  4.     assume ds:filefix,ss:filefix,cs:filefix,es:filefix
  5.  
  6. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. ;  Buffer Data Base.
  8. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9. filfcb        equ 05ch
  10.  
  11. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  12. ;  Data Base Fix.
  13. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  14. filefix_start:
  15.  
  16.     push cs
  17.     pop ds                ;insure ds same as cs.
  18.     xor si,si
  19.     mov ch,40
  20.     mov bx,offset filfcb
  21. filefix_04:
  22.     mov al,byte ptr es:[bx]        ;copy file control block
  23.     mov byte ptr inputfcb[si],al
  24.     inc bx
  25.     inc si
  26.     dec ch
  27.     jnz filefix_04
  28.  
  29.     push ds
  30.     pop es                ;set es:
  31.     mov si,9
  32. filefix_08:
  33.     mov al,byte ptr inputfcb-1[si]
  34.     mov byte ptr outputfcb-1[si],al
  35.     dec si
  36.     jnz filefix_08
  37.  
  38. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  39. ;  attempt file open.
  40. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  41.     mov ah,15
  42.     mov dx,offset inputfcb
  43.     mov word ptr inputfcb+33,0    ;set record address to zero.
  44.     int 21h                ;open file
  45.     cmp al,-1            ;file available ?
  46.     jz dos_exit_cancel        ;no -->
  47.  
  48.     mov ah,22
  49.     mov dx,offset outputfcb
  50.     mov word ptr outputfcb+33,0    ;set record address to zero.
  51.     int 21h                ;create output file
  52.     cmp al,-1            ;file available ?
  53.     jz dos_exit_cancel        ;no -->
  54.  
  55.     mov byte ptr inputbuffer-1,-1
  56.     mov word ptr outputbuffer-2,0
  57.     jmp short filefix_20
  58.  
  59. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  60. ;  Exit
  61. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  62. dos_exit_cancel:
  63.     mov ax,offset 4c00h
  64.     int 21h
  65.  
  66. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  67. ;  read characters...
  68. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  69. filefix_20:
  70.     call filefix_getcharacter
  71.     jc filefix_40            ;if all done -->
  72.     cmp al,"Q"-40h
  73.     jz filefix_20            ;ignore ctl-Q -->
  74.     cmp al,"S"-40h
  75.     jz filefix_20            ;ignore ctl-S -->
  76.     cmp al,"M"-40h
  77.     jz filefix_24            ;ignore ctl-m -->
  78.     cmp al,"J"-40h
  79.     jz filefix_24            ;ignore ctl-j -->
  80.  
  81.     call filefix_storecharacter
  82.     jmp filefix_20
  83.  
  84. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  85. ;  fix end of line.
  86. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  87. filefix_24:
  88.     call filefix_getcharacter
  89.     cmp al,"Q"-40h
  90.     jz filefix_24            ;ignore ctl-Q -->
  91.     cmp al,"S"-40h
  92.     jz filefix_24            ;ignore ctl-S -->
  93.     cmp al,"M"-40h
  94.     jz filefix_24            ;ignore ctl-m -->
  95.     cmp al,"J"-40h
  96.     jz filefix_24            ;ignore ctl-j -->
  97.  
  98.     push ax
  99.     mov al,"M"-40h
  100.     call filefix_storecharacter
  101.     mov al,"J"-40h
  102.     call filefix_storecharacter
  103.     pop ax
  104.     call filefix_storecharacter
  105.     jmp filefix_20
  106.  
  107. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  108. ;  all done.
  109. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  110. filefix_40:
  111.     mov al,"M"-40h
  112.     call filefix_storecharacter
  113.     mov al,"J"-40h
  114.     call filefix_storecharacter
  115.     mov al,"Z"-40h
  116.     call filefix_storecharacter
  117.  
  118.     call filefix_writebuffer
  119.  
  120.     mov ah,16
  121.     mov dx,offset outputfcb
  122.     int 21h                ;close write file.
  123.  
  124.     mov ah,19
  125.     mov dx,offset inputfcb
  126.     int 21h                ;delete old (input) file.
  127.  
  128.     mov si,offset 11
  129. filefix_44:
  130.     mov al,byte ptr inputfcb[si]
  131.     mov byte ptr outputfcb+16[si],al
  132.     dec si
  133.     jnz filefix_44
  134.  
  135.     mov ah,23
  136.     mov dx,offset outputfcb
  137.     int 21h
  138.     jmp dos_exit_cancel
  139.  
  140. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  141. ;  get character from InputBuffer.
  142. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  143. filefix_getcharacter:
  144.     mov al,byte ptr inputbuffer-1
  145.     cmp al,-1            ;buffer empty (non-init) ?
  146.     jnz filefix_get_04        ;no, go get character -->
  147.     call filefix_readrecord
  148.  
  149. filefix_get_04:
  150.     xor bx,bx
  151.     mov bl,byte ptr inputbuffer-1
  152.     mov al,byte ptr inputbuffer[bx]
  153.     cmp al,"Z"-40h            ;control Z ?
  154.     stc                ;yes, set flag
  155.     jz filefix_get_ret        ;don't do another read -->
  156.  
  157.     inc byte ptr inputbuffer-1
  158.     cmp bl,127
  159.     jnz filefix_get_08        ;exit -->
  160.     push ax
  161.     call filefix_readrecord
  162.     pop ax
  163.  
  164. filefix_get_08:
  165.     or al,al
  166.  
  167. filefix_get_ret:
  168.     ret
  169.  
  170. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  171. ;  Read File
  172. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  173. filefix_readrecord:
  174.     mov ah,26
  175.     mov dx,offset inputbuffer
  176.     int 21h
  177.     mov ah,20
  178.     mov dx,offset inputfcb
  179.     int 21h
  180.     or al,al
  181.     jz filefix_readrecord_08
  182.     dec al                ;error code 1 ?
  183.     jz filefix_readrecord_06    ;yes, entire record at end of file -->
  184.  
  185.     mov bx,offset 128
  186.  
  187. filefix_readrecord_04:
  188.     or bx,bx
  189.     jz filefix_readrecord_08
  190.     dec bx
  191.     mov al,byte ptr inputbuffer-1[bx]
  192.     or al,al
  193.     jnz filefix_readrecord_08
  194.     mov byte ptr inputbuffer-1[bx],"Z"-40h
  195.     jmp filefix_readrecord_04
  196.  
  197. filefix_readrecord_06:
  198.     mov byte ptr inputbuffer,"Z"-40h
  199.  
  200. filefix_readrecord_08:
  201.     inc word ptr inputfcb+33
  202.     mov byte ptr inputbuffer-1,0
  203.     ret
  204.  
  205. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  206. ;  Store Character.
  207. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  208. filefix_storecharacter:
  209.     push ax
  210.     mov ah,"Z"-40h            ;automatically patch end of file
  211.     mov bx,word ptr outputbuffer-2
  212.     mov word ptr outputbuffer[bx],ax;store character(s)
  213.     inc word ptr outputbuffer-2
  214.  
  215.     cmp bx,offset 128*128-1        ;sixteen K?
  216.     jnz filefix_storecharacter_08
  217.     call filefix_writebuffer
  218.  
  219. filefix_storecharacter_08:
  220.     pop ax
  221.     ret
  222.  
  223. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  224. ;  output buffer (may be partially filled).
  225. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  226. filefix_writebuffer:
  227.     mov ax,word ptr outputbuffer-2
  228.     or ax,ax            ;anything to write ?
  229.     jz filefix_writebuffer_return    ;no -->
  230.  
  231.     mov dx,offset outputbuffer
  232.  
  233. filefix_writebuffer_04:
  234.     push dx
  235.     mov ah,26
  236.     int 21h
  237.  
  238.     mov ah,34
  239.     mov dx,offset outputfcb
  240.     int 21h
  241.     inc word ptr outputfcb+33
  242.     pop dx
  243.     add dx,offset 128
  244.     sub word ptr outputbuffer-2,128
  245.     jz filefix_writebuffer_return
  246.     cmp word ptr outputbuffer-2,128
  247.     jnc filefix_writebuffer_04
  248.  
  249.     mov word ptr outputbuffer-2,128
  250.     jmp filefix_writebuffer_04
  251.  
  252. filefix_writebuffer_return:
  253.     mov word ptr outputbuffer-2,0
  254.     ret
  255.  
  256. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  257. ;  Buffer Data Base.
  258. ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  259. inputfcb:    db 40 dup(0)
  260. outputfcb:    db 0,"________$$$",30 dup(0)
  261.  
  262.         db -1
  263. inputbuffer:    db 128 dup(0)
  264.  
  265.         dw 0
  266. outputbuffer:    db 128 dup(0)        ;actually, goes for many K...
  267.  
  268. filefix    ends
  269.     end filefix_start
  270.  
  271.